package br.com.seosoft.simplecalcfinan.utils;

import java.util.regex.Pattern;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.Patterns;
import br.com.seosoft.simplecalcfinan.R;
import br.com.seosoft.simplecalcfinan.geral;

public class Propaganda {

	private String cliente;
	// preferences
	private SharedPreferences preference;
	private SharedPreferences.Editor editor;

	public boolean verificarClienteJaPago(Activity act) {

		String[] emailsValidos = act.getResources().getStringArray(
				R.array.emailsClientesQueJaPago);

		String[] possiveisEmails;

		boolean clienteValido = false;
		Log.v(geral.LogCat, "*** Verificando Clientes que ja pagaram o app ***");
		Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
		Account[] accounts = AccountManager.get(act).getAccounts();

		possiveisEmails = new String[accounts.length + 10];

		int count = 0;

		for (Account account : accounts) {

			if (emailPattern.matcher(account.name).matches()) {
				count++;
				possiveisEmails[count] = account.name;

				if (account.name.equals("pedrokra@gmail.com")
						|| account.name.equals("raphaeldesouzan@gmail.com")) {

					// dialogUsuarioPago();

				}

				Log.v(geral.LogCat, "**** Email: " + possiveisEmails[count]);

				for (int i = 0; i < emailsValidos.length; i++) {

					if (emailsValidos[i].equals(account.name)) {
						clienteValido = true;
						Log.v(geral.LogCat, "**** Email CLIENTE VALIDO: "
								+ possiveisEmails[count]);

						cliente = account.name;
						return clienteValido;

					}

				}

			}
		}

		if (isRemoveAds(act)) {
			clienteValido = true;
		}

		return clienteValido;

	}

	// Verifica se esta paga a remocao das propagadas
	public boolean isRemoveAds(Activity act) {
		// TODO Auto-generated method stub

		// Obtem preferencias salvas
		preference = PreferenceManager.getDefaultSharedPreferences(act);

		editor = preference.edit();
		return preference.getBoolean(geral.REMOVEU_ADS, false);

	}

	public String getCliente() {
		return cliente;
	}

}
